<?php
#if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

define( "LOG__NONE",    0 );
define( "LOG__ERROR",   1 );
define( "LOG__VERBOSE", 2 );
define( "LOG__DEBUG",   3 );

define( "STATUS_OK",       0 );
define( "STATUS_WARNING",  1 );
define( "STATUS_CRITICAL", 2 );
define( "STATUS_UNKNOWN",  3 );

#Sortie possible 

$criticals = "";
$warnings  = "";
$unknowns  = "";
$status  = "";
$args = array();
$ini_array = array();

$path = realpath(dirname(__FILE__));
if (file_exists($path.'/ws.ini')) {
	$ini_array = parse_ini_file($path.'/ws.ini', true);
	#_log( var_dump($ini_array) , LOG__DEBUG );
}
#Configuration des options 
$cmdargs = array(
    'config_version'     => '1.0',
	'file_log'			=> $path.'/'.$ini_array['General']['file_log'],
    'log_level'          => $ini_array['General']['log_level'],
);
$log_level = LOG__NONE;
$log=$path.'/'.$ini_array['General']['file_log'];
#Decoupage des arguments 
$cmd = explode(",", $argv[1]); 
#_log( var_dump($cmd)."\n", LOG__DEBUG );
#Mise dans l ordre  des arguments
sort($cmd);
#_log( "Nombre d'arguments : ".(count($cmd))."", LOG__DEBUG );
  
# Decoupage par arguments
for ($idx=0 ; $idx<count($cmd) ; $idx++){
	$cmd_filtre[] = explode(".", $cmd[$idx] );	
}
#_log( var_dump($cmd_filtre), LOG__DEBUG );

# Trie du tableau 
# Decoupage par fournisseur 

foreach($cmd_filtre as $idx => $filtre){
	$args[$cmd_filtre[$idx][0]] .= $cmd_filtre[$idx][0].".".$cmd_filtre[$idx][1].".".$cmd_filtre[$idx][2].",";
}	
#_log( var_dump($args), LOG__DEBUG );

#for ($idx=0 ; $idx<count($cmd) ; $idx++){
#	$args[$cmd_filtre[$idx][0]] .= $cmd_filtre[$idx][0].".".$cmd_filtre[$idx][1].".".$cmd_filtre[$idx][2].",";		
#}
##_log( count($args), LOG__DEBUG );
 foreach($args as $cle => $valeur){
	$valeur = substr($valeur,0,-1);
	#$cle = substr($valeur,1,1);
	_log( $cle.' : '.$valeur, LOG__DEBUG );	
	switch ($argv[2]) {
    case "inquiry":		
		$exec = 'php '.$path.'/'.$ini_array['provider'][$cle].' '.$valeur.' inquiry ';
		_log( "Executing: $exec", LOG__ERROR );
		$result = exec( $exec );
		_log( "Result: ".$result."", LOG__ERROR );
		$output = explode("|", $result);		
		$valid[$cle]['status'] = $output[0]; 
		$valid[$cle]['message'] = $output[1];
		$valid[$cle]['type'] = $output[2];
		$valid[$cle]['name'] = $output[3];
    break;
    case "order":		
		$exec = 'php '.$path.'/'.$ini_array['provider'][$cle].' '.$valeur.' order "'.$argv[3].'"';
		_log( "Executing: $exec", LOG__ERROR );
		$result = exec( $exec );
		_log( "Result: ".$result."", LOG__ERROR );
		$output = explode("|", $result);
		$valid[$cle]['status'] = $output[0]; 
		$valid[$cle]['message'] = $output[1];
		$valid[$cle]['type'] = $output[2];
		$valid[$cle]['name'] = $output[3];
	break;
	default:
        setStatus( STATUS_UNKNOWN );
    break;
	}	
}
#Verification  de l'etat de chaque  SOAP fournisseur
#var_dump($valid);
foreach($valid as $index => $string){
	if (preg_match("#OK#", $valid[$index]['message'])) { 
		setStatus( STATUS_OK );
	}elseif(preg_match("#WARNING#", $valid[$index]['status'])) {
		setStatus( STATUS_WARNING );
		$warnings .= $valid[$index]['message'];
	}elseif(preg_match("#CRITICAL#", $valid[$index]['status'])) {
		setStatus( STATUS_CRITICAL );
		$critical .= $valid[$index]['message'];
	}	
}	

foreach($valid as $index => $string){	
		$msg  .= $valid[$index]['status']."|".$valid[$index]['message']."|".$valid[$index]['type']."|".$valid[$index]['name']."!";			
}
$msg = substr($msg,0,-1);
/*if( $status == STATUS_OK )	
    echo $msg = "OK|OK\n";
else
    $msg = "WARNING|{$criticals}{$warnings}{$unknowns}\n";
*/
echo $msg;
exit( $status );


function _log( $log, $level )
{
    global $cmdargs;

    if( $level == LOG__ERROR ){
		$fp = fopen($cmdargs['file_log'], 'w');
        fwrite( $fp, $log."\n" );
		fclose($fp);
    }else if( $level <= $cmdargs['log_level'] ){
        print( $log . "\n" );
	}
}

function setStatus( $new_status )
{
    global $status;
    if( $new_status > $status )
        $status = $new_status;
}




?>